---
- name: Backup ALL
  hosts: all
  connection: network_cli
  gather_facts: false

  tasks:
    - name: Accespt ansible net hostname
      ios_facts:
        gather_subset: all

    - name: Get current date
      local_action: command date +%Y-%b-%d
      register: date

    - name: Get current time
      local_action: command date +%H:%M
      register: time

    - name: Get running-config and save it
      ios_config:
        backup: yes
        backup_options:
          filename: "{{ ansible_net_hostname }}_{{ ansible_host }}.cfg"
          dir_path: /home/INFRASTRUCTURE/{{ date.stdout }}_at_{{ time.stdout }}

    - name: Tasks done
      debug: msg="All tasks has been done on {{ date.stdout }} at {{ time.stdout }}."

- name: config ACCESS switch
  hosts: "{{ CUSTOMER_SWITCH_NUMBER }}"
  become: yes

  tasks:
    - name: add vlan
      cisco.ios.ios_vlans:
        config:
          - name: "{{ CUSTOMER_VLAN_NAME }}"
            vlan_id: "{{ CUSTOMER_VLAN_NUMBER }}"
            state: active
            shutdown: disabled
            remote_span: true
        state: merged

    - name: add vlan to interfaces with access
      cisco.ios.ios_l2_interfaces:
        config:
          - name: Ethernet3/0
            mode: access
            access:
              vlan: "{{ CUSTOMER_VLAN_NUMBER }}"
          - name: Ethernet3/1
            mode: access
            access:
              vlan: "{{ CUSTOMER_VLAN_NUMBER }}"
          - name: Ethernet3/2
            mode: access
            access:
              vlan: "{{ CUSTOMER_VLAN_NUMBER }}"
          - name: Ethernet3/3
            mode: access
            access:
              vlan: "{{ CUSTOMER_VLAN_NUMBER }}"
        state: merged

    - name: add spanning-tree
      cisco.ios.ios_config:
        lines: spanning-tree mode pvst

    - name: add default router ip
      cisco.ios.ios_config:
        lines: "{{ DEFAULT_ROUTER_IP }}"

    - name: configure interface settings
      cisco.ios.ios_config:
        lines:
          - description "{{ CUSTOMER_VLAN_NAME }}"
          - ip address "{{ CUSTOMER_INT_VLAN_IP }}" "{{ CUSTOMER_VLAN_MASK }}"
          - no shutdown
        parents: interface vlan"{{ CUSTOMER_VLAN_NUMBER }}"

- name: CHANGE IP ON INTs VLANs of DIST1
  hosts: dist1
  become: yes

  tasks:
    - name: add vlan
      cisco.ios.ios_vlans:
        config:
          - name: "{{ CUSTOMER_VLAN_NAME }}"
            vlan_id: "{{ CUSTOMER_VLAN_NUMBER }}"
            state: active
            shutdown: disabled
            remote_span: true
        state: merged

    - name: configure interface settings
      cisco.ios.ios_config:
        lines:
          - description "{{ CUSTOMER_VLAN_NAME }}"
          - ip address "{{ CUSTOMER_INT_VLAN_IP_DIST1 }}" "{{ CUSTOMER_VLAN_MASK }}"
          - no shutdown
          - vrrp "{{ CUSTOMER_VLAN_NUMBER }}" priority 150
          - vrrp "{{ CUSTOMER_VLAN_NUMBER }}" ip "{{ CUSTOMER_VRRP_IP }}"
           - vrrp "{{ CUSTOMER_VLAN_NUMBER }}" preemp
        parents: interface vlan"{{ CUSTOMER_VLAN_NUMBER }}"

    - name: save config
      cisco.ios.ios_command:
        commands: wr

- name: CHANGE IP ON INTs VLANs of DIST2
  hosts: dist2
  become: yes

  tasks:
    - name: add vlan
      cisco.ios.ios_vlans:
        config:
          - name: "{{ CUSTOMER_VLAN_NAME }}"
            vlan_id: "{{ CUSTOMER_VLAN_NUMBER }}"
            state: active
            shutdown: disabled
            remote_span: true
        state: merged

    - name: configure interface settings
      cisco.ios.ios_config:
        lines:
          - description "{{ CUSTOMER_VLAN_NAME }}"
          - ip address "{{ CUSTOMER_INT_VLAN_IP_DIST2 }}" "{{ CUSTOMER_VLAN_MASK }}"
          - no shutdown
          - vrrp "{{ CUSTOMER_VLAN_NUMBER }}" priority 100
          - vrrp "{{ CUSTOMER_VLAN_NUMBER }}" ip "{{ CUSTOMER_VRRP_IP }}"
        parents: interface vlan"{{ CUSTOMER_VLAN_NUMBER }}"

- name: SAVE ALL
  hosts: all
  become: yes

  tasks:
    - name: save configs
      cisco.ios.ios_command:
        commands: wr
